* This script contains the code to replicate TableC of the on-line appendix in Ductor, L., Fafchamps, M., Goyal S. and M. van der Leij. Social Networks and Research Output. The Review of Economics and Statistics.
/* Considering Panel data specifications*/
log using TableC.log, replace

set more off
use prodndnp_fullsample.dta


/*Unrestricted models*/
/*MV Model 3: All past output + All relevance network variables*/
/*FD out-of-sample program, Model 3 and Model 1 will be inconsistent*/
cap pro drop fd_os
program define fd_os
tempvar e yhat efd sqerrfd pefd dyhat 
syntax varlist(numeric min=1) [if]
marksample touse
quietly reg D.(`varlist') if `touse', vce(robust)
qui scalar n1=e(N)
estat ic
quietly gen efdi=1 if e(sample)
quietly predict dyhat if group==2 & lprodf3<., xb /*predictions of the first difference*/
quietly gen ybfd=dyhat+L.dyhat+L2.dyhat+L3.lprodf3 /*out-of-sample prediction including individual fixed effects*/
/*Obtaining the out-of-sample RMSE for the FD model*/
quietly gen efdo=1 if ybfd<.
quietly gen sqerrfd= (lprodf3-ybfd)^2 if ybfd<.
quietly egen pefd=sum(sqerrfd)if ybfd<.  /*RSS of the predicted model*/
quietly sum pefd    /*summarizing the RSS*/
quietly scalar rssfd=r(max)  /*obtaining the total RSS*/
quietly scalar nfd=r(N)    /*number of observations in group 2*/
quietly scalar rmseo2fd=(rssfd/nfd)^0.5   /*RMSE out of sample*/
di n1 " Number of observations in the in-sample group"
di rmseo2fd "  FD Out-of-sample RMSE"
di nfd " Number of observations in the out-of-sample group"
drop dyhat sqerrfd pefd
end

/*OLS out-of-sample program*/
cap pro drop ols_os
program define ols_os
syntax varlist(numeric min=1) [if]
marksample touse
quietly reg `varlist' if `touse', vce(cluster auth)
qui scalar n1=e(N)
estat ic
scalar rmseols=e(rmse)
predict ybols if efdo==1., xb 
scalar r2i1ols= e(r2)   /*computing the R2 of the baseline model*/
qui gen sqerrols= (lprodf3-ybols)^2 if ybols<.
quietly egen peols=sum(sqerrols)if ybols<.  /*RSS of the predicted model*/
quietly sum peols    /*summarizing the RSS*/
quietly scalar rssols=r(max)  /*obtaining the total RSS*/
quietly scalar nols=r(N)    /*number of observations in group 2*/
scalar rmseo2ols=(rssols/nols)^0.5   /*RMSE out of sample*/
di rmseols   "  Pooled OLS in-sample RMSE"
di n1 " Number of observations in the in-sample group"
di rmseo2ols "  Pooled OLS out-of-sample RMSE"
di nols " Number of observations in the out-of-sample group"
drop sqerrols peols
end


fd_os lprodf3 t3-t27 y3-y27 lprodf3l-lprodf3l13 nopapers lnetprod1y-lnetprod12y lnetprod21y-lnetprod212y degree1y-degree12y degree21y-degree212y gc1y-gc12y lbet1y-lbet12y lclos1y-lclos12y neiq1fs1y-neiq1fs12y if group==1
ols_os lprodf3 t3-t27 y3-y27 lprodf3l-lprodf3l13 nopapers lnetprod1y-lnetprod12y lnetprod21y-lnetprod212y degree1y-degree12y degree21y-degree212y gc1y-gc12y lbet1y-lbet12y lclos1y-lclos12y neiq1fs1y-neiq1fs12y  if efdi==1 
xtabond2 lprodf3 t3-t27 y3-y27 lprodf3l-lprodf3l13 nopapers lnetprod1y-lnetprod12y lnetprod21y-lnetprod212y degree1y-degree12y degree21y-degree212y gc1y-gc12y lbet1y-lbet12y lclos1y-lclos12y neiq1fs1y-neiq1fs12y  if efdi==1, gmm(lprodf3l-lprodf3l13, laglimit(14 15) collapse) iv(t3-t27 y3-y27  nopapers lnetprod1y-lnetprod12y lnetprod21y-lnetprod212y degree1y-degree12y degree21y-degree212y gc1y-gc12y lbet1y-lbet12y lclos1y-lclos12y neiq1fs1y-neiq1fs12y) twostep robust 
qui scalar n1=e(N)
quietly predict ybar if ybfd<., xb /*predictions of the first difference*/
/*Obtaining the out-of-sample RMSE for the System GMM model*/
quietly gen sqerrar= (lprodf3-ybar)^2 if ybar<.
quietly egen pear=sum(sqerrar)if ybar<.  /*RSS of the predicted model*/
quietly sum pear    /*summarizing the RSS*/
quietly scalar rssar=r(max)  /*obtaining the total RSS*/
quietly scalar nar=r(N)    /*number of observations in group 2*/
quietly scalar rmseo2ar=(rssar/nar)^0.5   /*RMSE out of sample*/
di n1 " Number of observations in the in-sample group"
di rmseo2ar "  FD Out-of-sample RMSE"
di nar " Number of observations in the out-of-sample group"
drop sqerrar pear ybfd ybols efdi ybar efdo
 
/*MV Model 2: Past output till t-5 + All relevance network variables*/

fd_os lprodf3 t3-t27 y3-y27 nopapers lnetprod1y-lnetprod12y lnetprod21y-lnetprod212y degree1y-degree12y degree21y-degree212y gc1y-gc12y lbet1y-lbet12y lclos1y-lclos12y neiq1fs1y-neiq1fs12y if group==1
ols_os lprodf3 t3-t27 y3-y27 nopapers lnetprod1y-lnetprod12y lnetprod21y-lnetprod212y degree1y-degree12y degree21y-degree212y gc1y-gc12y lbet1y-lbet12y lclos1y-lclos12y neiq1fs1y-neiq1fs12y  if efdi==1 
qui scalar n1=e(N)
quietly predict ybar if ybfd<., xb /*predictions of the first difference*/
/*Obtaining the out-of-sample RMSE for the System GMM model*/
quietly gen sqerrar= (lprodf3-ybar)^2 if ybar<.
quietly egen pear=sum(sqerrar)if ybar<.  /*RSS of the predicted model*/
quietly sum pear    /*summarizing the RSS*/
quietly scalar rssar=r(max)  /*obtaining the total RSS*/
quietly scalar nar=r(N)    /*number of observations in group 2*/
quietly scalar rmseo2ar=(rssar/nar)^0.5   /*RMSE out of sample*/
di n1 " Number of observations in the in-sample group"
di rmseo2ar "  FD Out-of-sample RMSE"
di nar " Number of observations in the out-of-sample group"
drop sqerrar pear ybfd ybols efdi efdo ybar

/*Baseline model:*/

fd_os lprodf3 t3-t27 y3-y27 nopapers lprodf3l-lprodf3l13 if group==1
ols_os lprodf3 t3-t27 y3-y27 nopapers lprodf3l-lprodf3l13 if efdi==1 
xtabond2 lprodf3 t3-t27 y3-y27 nopapers lprodf3l-lprodf3l13 if efdi==1, gmm(lprodf3l-lprodf3l13, lag(14 15)) iv(t3-t27 y3-y27 nopapers) twostep robust 
qui scalar n1=e(N)
quietly predict ybar if ybfd<., xb /*predictions of the first difference*/
/*Obtaining the out-of-sample RMSE for the System GMM model*/
quietly gen sqerrar= (lprodf3-ybar)^2 if ybar<.
quietly egen pear=sum(sqerrar)if ybar<.  /*RSS of the predicted model*/
quietly sum pear    /*summarizing the RSS*/
quietly scalar rssar=r(max)  /*obtaining the total RSS*/
quietly scalar nar=r(N)    /*number of observations in group 2*/
quietly scalar rmseo2ar=(rssar/nar)^0.5   /*RMSE out of sample*/
di n1 " Number of observations in the in-sample group"
di rmseo2ar "  FD Out-of-sample RMSE"
di nar " Number of observations in the out-of-sample group"
drop sqerrar pear ybfd ybols efdi efdo ybar

log close
